// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.inventory;

import java.util.ArrayList;
import java.util.List;
import zombie.characters.IsoGameCharacter;
import zombie.characters.skills.PerkFactory;
import zombie.core.Rand;
import zombie.inventory.types.DrainableComboItem;
import zombie.inventory.types.HandWeapon;
import zombie.scripting.ScriptManager;
import zombie.scripting.objects.Fixing;

public final class FixingManager {
    public static ArrayList<Fixing> getFixes(InventoryItem item) {
        ArrayList arrayList = new ArrayList();
        List list = ScriptManager.instance.getAllFixing(new ArrayList());

        for (int _int = 0; _int < list.size(); _int++) {
            Fixing fixing = (Fixing)list.get(_int);
            if (fixing.getRequiredItem().contains(item.getType())) {
                arrayList.add(fixing);
            }
        }

        return arrayList;
    }

    public static InventoryItem fixItem(InventoryItem brokenItem, IsoGameCharacter chr, Fixing fixing, Fixing.Fixer fixer) {
        if ((double)Rand.Next(100) >= getChanceOfFail(brokenItem, chr, fixing, fixer)) {
            double _doublex = getCondRepaired(brokenItem, chr, fixing, fixer);
            int int0 = brokenItem.getConditionMax() - brokenItem.getCondition();
            Double _doublex = new Double((double)int0 * (_doublex / 100.0));
            int int1 = (int)Math.round(_doublex);
            if (int1 == 0) {
                int1 = 1;
            }

            brokenItem.setCondition(brokenItem.getCondition() + int1);
            brokenItem.setHaveBeenRepaired(brokenItem.getHaveBeenRepaired() + 1);
        } else if (brokenItem.getCondition() > 0 && Rand.Next(5) == 0) {
            brokenItem.setCondition(brokenItem.getCondition() - 1);
            chr.getEmitter().playSound("FixingItemFailed");
        }

        useFixer(chr, fixer, brokenItem);
        if (fixing.getGlobalItem() != null) {
            useFixer(chr, fixing.getGlobalItem(), brokenItem);
        }

        addXp(chr, fixer);
        return brokenItem;
    }

    private static void addXp(IsoGameCharacter isoGameCharacter, Fixing.Fixer fixer) {
        if (fixer.getFixerSkills() != null) {
            for (int _int = 0; _int < fixer.getFixerSkills().size(); _int++) {
                Fixing.FixerSkill fixerSkill = (Fixing.FixerSkill)fixer.getFixerSkills().get(_int);
                isoGameCharacter.getXp().AddXP(PerkFactory.Perks.FromString(fixerSkill.getSkillName()), (float)Rand.Next(3, 6));
            }
        }
    }

    public static void useFixer(IsoGameCharacter chr, Fixing.Fixer fixer, InventoryItem brokenItem) {
        int int0 = fixer.getNumberOfUse();

        for (int int1 = 0; int1 < chr.getInventory().getItems().size(); int1++) {
            if (brokenItem != chr.getInventory().getItems().get(int1)) {
                InventoryItem inventoryItem0 = (InventoryItem)chr.getInventory().getItems().get(int1);
                if (inventoryItem0 != null && inventoryItem0.getType().equals(fixer.getFixerName())) {
                    if (inventoryItem0 instanceof DrainableComboItem) {
                        if ("DuctTape".equals(inventoryItem0.getType()) || "Scotchtape".equals(inventoryItem0.getType())) {
                            chr.getEmitter().playSound("FixWithTape");
                        }

                        int int2 = ((DrainableComboItem)inventoryItem0).getDrainableUsesInt();
                        int int3 = Math.min(int2, int0);

                        for (int int4 = 0; int4 < int3; int4++) {
                            inventoryItem0.Use();
                            int0--;
                            if (!chr.getInventory().getItems().contains(inventoryItem0)) {
                                int1--;
                                break;
                            }
                        }
                    } else {
                        if (inventoryItem0 instanceof HandWeapon) {
                            if (chr.getSecondaryHandItem() == inventoryItem0) {
                                chr.setSecondaryHandItem(null);
                            }

                            if (chr.getPrimaryHandItem() == inventoryItem0) {
                                chr.setPrimaryHandItem(null);
                            }

                            HandWeapon handWeapon = (HandWeapon)inventoryItem0;
                            if (handWeapon.getScope() != null) {
                                chr.getInventory().AddItem(handWeapon.getScope());
                            }

                            if (handWeapon.getClip() != null) {
                                chr.getInventory().AddItem(handWeapon.getClip());
                            }

                            if (handWeapon.getSling() != null) {
                                chr.getInventory().AddItem(handWeapon.getSling());
                            }

                            if (handWeapon.getStock() != null) {
                                chr.getInventory().AddItem(handWeapon.getStock());
                            }

                            if (handWeapon.getCanon() != null) {
                                chr.getInventory().AddItem(handWeapon.getCanon());
                            }

                            if (handWeapon.getRecoilpad() != null) {
                                chr.getInventory().AddItem(handWeapon.getRecoilpad());
                            }

                            int int5 = 0;
                            if (handWeapon.getMagazineType() != null && handWeapon.isContainsClip()) {
                                InventoryItem inventoryItem1 = InventoryItemFactory.CreateItem(handWeapon.getMagazineType());
                                inventoryItem1.setCurrentAmmoCount(handWeapon.getCurrentAmmoCount());
                                chr.getInventory().AddItem(inventoryItem1);
                            } else if (handWeapon.getCurrentAmmoCount() > 0) {
                                int5 += handWeapon.getCurrentAmmoCount();
                            }

                            if (handWeapon.haveChamber() && handWeapon.isRoundChambered()) {
                                int5++;
                            }

                            if (int5 > 0) {
                                for (int int6 = 0; int6 < int5; int6++) {
                                    InventoryItem inventoryItem2 = InventoryItemFactory.CreateItem(handWeapon.getAmmoType());
                                    chr.getInventory().AddItem(inventoryItem2);
                                }
                            }
                        }

                        chr.getInventory().Remove(inventoryItem0);
                        int1--;
                        int0--;
                    }
                }

                if (int0 == 0) {
                    break;
                }
            }
        }
    }

    public static double getChanceOfFail(InventoryItem brokenItem, IsoGameCharacter chr, Fixing fixing, Fixing.Fixer fixer) {
        double _double = 3.0;
        if (fixer.getFixerSkills() != null) {
            for (int _int = 0; _int < fixer.getFixerSkills().size(); _int++) {
                if (chr.getPerkLevel(PerkFactory.Perks.FromString(((Fixing.FixerSkill)fixer.getFixerSkills().get(_int)).getSkillName()))
                    < ((Fixing.FixerSkill)fixer.getFixerSkills().get(_int)).getSkillLevel()) {
                    _double += (double)(
                        (
                                ((Fixing.FixerSkill)fixer.getFixerSkills().get(_int)).getSkillLevel()
                                    - chr.getPerkLevel(PerkFactory.Perks.FromString(((Fixing.FixerSkill)fixer.getFixerSkills().get(_int)).getSkillName()))
                            )
                            * 30
                    );
                } else {
                    _double -= (double)(
                        (
                                chr.getPerkLevel(PerkFactory.Perks.FromString(((Fixing.FixerSkill)fixer.getFixerSkills().get(_int)).getSkillName()))
                                    - ((Fixing.FixerSkill)fixer.getFixerSkills().get(_int)).getSkillLevel()
                            )
                            * 5
                    );
                }
            }
        }

        _double += (double)(brokenItem.getHaveBeenRepaired() * 2);
        if (chr.Traits.Lucky.isSet()) {
            _double -= 5.0;
        }

        if (chr.Traits.Unlucky.isSet()) {
            _double += 5.0;
        }

        if (_double > 100.0) {
            _double = 100.0;
        }

        if (_double < 0.0) {
            _double = 0.0;
        }

        return _double;
    }

    public static double getCondRepaired(InventoryItem brokenItem, IsoGameCharacter chr, Fixing fixing, Fixing.Fixer fixer) {
        double _double = 0.0;

        _double = switch (fixing.getFixers().indexOf(fixer)) {
            case 0 -> 50.0 * (1.0 / (double)brokenItem.getHaveBeenRepaired());
            case 1 -> 20.0 * (1.0 / (double)brokenItem.getHaveBeenRepaired());
            default -> 10.0 * (1.0 / (double)brokenItem.getHaveBeenRepaired());
        };
        if (fixer.getFixerSkills() != null) {
            for (int int0 = 0; int0 < fixer.getFixerSkills().size(); int0++) {
                Fixing.FixerSkill fixerSkill = (Fixing.FixerSkill)fixer.getFixerSkills().get(int0);
                int int1 = chr.getPerkLevel(PerkFactory.Perks.FromString(fixerSkill.getSkillName()));
                if (int1 > fixerSkill.getSkillLevel()) {
                    _double += (double)Math.min((int1 - fixerSkill.getSkillLevel()) * 5, 25);
                } else {
                    _double -= (double)((fixerSkill.getSkillLevel() - int1) * 15);
                }
            }
        }

        _double *= (double)fixing.getConditionModifier();
        _double = Math.max(0.0, _double);
        return Math.min(100.0, _double);
    }
}
